﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using MX.Core.Log;
using MX.Core.ORM;
using UserCenter.Models;

namespace UserCenter.BLL
{
    public class RoleBLL
    {
        static public int Create(int merchantId, string key, string name, string icoUrl, string description)
        {
            using (var conn = DatabaseHelper.Get())
            {
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        string strSql = @"INSERT INTO  `role` ( 
                                          `MerchantId`,
                                          `Key`,
                                          `IcoUrl`,
                                          `RuleName`,
                                          `Description`
                                        ) 
                                        VALUES
                                          ( 
                                            @MerchantId,
                                            @Key,
                                            @IcoUrl,
                                            @RuleName,
                                            @Description
                                          )";
                        if (conn.Execute(strSql, new Role
                        {
                            Description = description,
                            IcoUrl = icoUrl,
                            Key = key,
                            MerchantId = merchantId,
                            RuleName = name
                        }, transaction: tran) > 0)
                        {
                            var roleId = conn.ExecuteScalar<int>("Select LAST_INSERT_ID()");
                            tran.Commit();
                            return roleId;
                        }
                        else
                        {
                            tran.Rollback();
                            return 0;
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.GetLogger(nameof(RoleBLL)).Error(ex, "");
                        tran.Rollback();
                        return 0;
                    }
                }
            }
        }

        static public Role Info(int merchantId, string key)
        {
            using (var conn = DatabaseHelper.Get())
            {
                return conn.QuerySingleOrDefault<Role>("select * from `role` where MerchantId=@MerchantId and `Key`=@Key", new
                {
                    Key = key,
                    MerchantId = merchantId
                });
            }
        }


        public static bool Update(int roleId, string name, string iconUrl, string description)
        {
            using (var conn = DatabaseHelper.Get())
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("UPDATE `role` set ");
                if (!string.IsNullOrEmpty(name))
                    strSql.Append("`RuleName` = @RuleName,");
                if (!string.IsNullOrEmpty(iconUrl))
                    strSql.Append("`IcoUrl` = @IcoUrl,");
                if (!string.IsNullOrEmpty(description))
                    strSql.Append("`Description` = @Description,");
                strSql.Remove(strSql.Length - 1, 1);
                strSql.Append(" where `RoleId` = @RoleId ");
                return conn.Execute(strSql.ToString(), new
                {
                    RuleName = name,
                    IcoUrl = iconUrl,
                    Description = description
                }) > 0;
            }
        }

        public static List<Role> List(int merchantId)
        {
            using (var conn = DatabaseHelper.Get())
                return conn.Query<Role>("select * from `role` where MerchantId=@MerchantId ", new
                {
                    MerchantId = merchantId
                }).AsList();
        }
    }
}
